package com.seas;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.seas.config.BookSetting;
import com.seas.dao.PersonRepository;
import com.seas.support.CustomRepositoryFactoryBean;
import org.omg.CORBA.portable.UnknownException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Seas Cheng on 2017/9/23
 */
@RestController
@EnableJpaRepositories(repositoryFactoryBeanClass = CustomRepositoryFactoryBean.class) //让自定义的Repository生效。没有自定义，不需要添加，SpringBootApplication内已经包含
@SpringBootApplication //Spring boot 核心注解，开启自动配置
public class Application {

    @Value("${book.author}")
    private String bookAuthor;
    @Value("${book.name}")
    private String bookName;
    @Autowired
    private BookSetting bookSetting;
    @Autowired
    PersonRepository personRepository;

    public static void main(String[] args) throws Exception{
        SpringApplication.run(Application.class, args); // Spring Boot会自动扫描@SpringBootApplication 所在类的同级包以及下级包里的Bean

    }

    @RequestMapping("/")
    String home(){
        return "Hello World";
    }

    //直接读取application.properties文件中的配置
    @RequestMapping("/applicationProperties")
    String applicationProperties(){
        return "book name:"+bookName+" book author:"+bookAuthor;
    }

    @RequestMapping("/typeSafetySetting")
    String typeSafetySetting(){
        return "book name:"+bookSetting.getName()+" book author:"+bookSetting.getAuthor();
    }

    //自定义RedisTemplate，springboot本身的RedisTemplate使用的是JdkSerializationRedisSerializer,二进制。现在改为json。
    @Bean
    @SuppressWarnings({"rawtypes", "unchecked"})
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownException{
        RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        template.setValueSerializer(jackson2JsonRedisSerializer); //值的序列化采用jackson2JsonRedisSerializer
        template.setKeySerializer(new StringRedisSerializer());  //key的序列化采用StringRedisSerializer
        template.setConnectionFactory(redisConnectionFactory);
        template.afterPropertiesSet();
        return template;

    }


}